﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfumeShopManagement.Model;
using System.Data.Linq;

namespace PerfumeShopManagement.DAL
{
    public class DalExportCheck : DalAbstractBase<ExportCheck, int>
    {
        public override ExportCheck GetObjectById(int id)
        {
            throw new NotImplementedException();
        }

        public override List<ExportCheck> GetAllObjects()
        {
            var result = (from e in _context.ExportChecks
                          select e);
            if (result != null)
                return result.ToList<ExportCheck>();

            return new List<ExportCheck>();
        }

        public override List<ExportCheck> GetObjectsByCriteria(ExportCheck obj)
        {
            throw new NotImplementedException();
        }

        public override bool Insert(ExportCheck obj)
        {
            ExportCheck exportCheck = obj;

            _context.ExportChecks.AddObject(exportCheck);
            _context.SaveChanges();

            return true;
        }

        public override bool Update(ExportCheck obj)
        {
            ExportCheck exportCheck = obj;

            // Delete all export check detail of the export check.
            var detailDeleting = (from e in _context.ExportCheckDetails
                                  where e.ExportCheckDetailId.Equals(exportCheck.ExportCheckId)
                                  select e);
            foreach (var item in detailDeleting)
            {
                _context.ExportCheckDetails.DeleteObject(item);
            }

            // Update the export check.
            ExportCheck exportCheckRow = _context.ExportChecks.Single(
                e => e.ExportCheckId == exportCheck.ExportCheckId);
            exportCheckRow.ExportCheckTime = exportCheck.ExportCheckTime;
            exportCheckRow.Username = exportCheck.Username;
            exportCheckRow.Total = exportCheck.Total;
            exportCheckRow.ExportCheckDetail = exportCheck.ExportCheckDetail;

            _context.SaveChanges();

            // Refresh the export check detail to return.
            foreach (ExportCheckDetail ex in exportCheckRow.ExportCheckDetail)
            {
                exportCheck.ExportCheckDetail.Add(ex);
            }

            return true;
        }

        public override bool DeleteById(int id)
        {
            bool deleted = false;
            int exportCheckId = id;
            var result = (from e in _context.ExportChecks
                          where e.ExportCheckId.Equals(exportCheckId)
                          select e).FirstOrDefault();

            if (result != null)
            {
                foreach (ExportCheckDetail ex in result.ExportCheckDetail)
                {
                    _context.ExportCheckDetails.DeleteObject(ex);
                }

                _context.ExportChecks.DeleteObject((ExportCheck)result);
                _context.SaveChanges();
                deleted = true;
            }

            return deleted;
        }
    }
}
